package org.example;

// 抽象产品类
abstract class Animal {
    public abstract String speak();
}

// 具体产品类
class Dog extends Animal {
    @Override
    public String speak() {
        return "Woof!";
    }
}

class Cat extends Animal {
    @Override
    public String speak() {
        return "Meow!";
    }
}

// 工厂类
class AnimalFactory {
    public static Animal createAnimal(String type) {
        if ("dog".equalsIgnoreCase(type)) {
            return new Dog();
        } else if ("cat".equalsIgnoreCase(type)) {
            return new Cat();
        } else {
            throw new IllegalArgumentException("Unknown animal type");
        }
    }
}

// 测试工厂模式
public class FactoryPattern {
    public static void main(String[] args) {
        Animal dog = AnimalFactory.createAnimal("dog");
        Animal cat = AnimalFactory.createAnimal("cat");

        System.out.println(dog.speak()); // 输出 "Woof!"
        System.out.println(cat.speak()); // 输出 "Meow!"
    }
}